<?php //strip
/**
 * Database model for reservations table.
 *
 * @package odReservationsWpPlugin
 * @subpackage odReservationsWpPlugin_Models
 * @author Ondrej Donek <ondrejd@gmail.com>
 */
class odReservationsWpPlugin_Models_Reservation extends odWpPlugin_Model_Simple
{
  protected $mMembersModel = null;
  protected $mPlaygroundsModel = null;
  
  public function __construct()
  {
    global $wpdb;
    
    $this->mTableName = $wpdb->prefix . 'reservations_reservations';
    $this->mPrimary = 'ID';
    $this->mCheckColumn = '';
    $this->mColumns = array(
      'ID' => array('type' => 'bigint', 'length' => 20, 'primary' => true),
      'member_ID' => array('type' => 'bigint', 'length' => 20, 'notnull' => true),
      // TODO Specifikace pro FOREIGN_KEY!!!
      'playground_ID' => array('type' => 'bigint', 'length' => 20, 'notnull' => true),
      'playmates' => array('type' => 'tinyint', 'length' => 1, 'notnull' => true, 'default' => 0, 'comment' => 'Playmates count'),
      'start' => array('type' => 'timestamp', 'notnull' => true, 'default' => 'CURRENT_TIMESTAMP'),
      'cancelled' => array('type' => 'tinyint', 'length' => 1, 'notnull' => true, 'default' => 0),
      'cancelled_when' => array('type' => 'timestamp', 'notnull' => false),
      'note' => array('type' => 'text', 'notnull' => true, 'default' => '')
    );
    
    $this->mMembersModel = new odReservationsWpPlugin_Models_Member();
    $this->mPlaygroundsModel = new odReservationsWpPlugin_Models_Playground();
  }
  
  public function select($aFilter = '', $aCount = 0, $aLimitFrom = 0)
  {
		global $wpdb;
		
		$reservations = parent::select($aFilter, $aCount, $aLimitFrom);
		if(is_null($reservations)) return array();
    
		foreach($reservations as $reservation_key => $reservation) {
      $member = $this->mMembersModel->select('`t1`.`ID` = ' . $reservation['member_ID'] . ' ');
      $playground = $this->mPlaygroundsModel->select('`t1`.`ID` = ' . $reservation['playground_ID'] . ' ');
      
      if(is_array($member) && count($member) == 1) {
        $reservations[$reservation_key]['member_num'] = $member[0]['member_num'];
        $reservations[$reservation_key]['member_name'] = $member[0]['fullname'];
      }
      
      if(is_array($playground) && count($playground) == 1) {
        $reservations[$reservation_key]['playground_name'] = $playground[0]['title'];
      }
		}
    
		return $reservations;
  }
  
} // End of odReservationsWpPlugin_Models_Reservation
